package com.example.module_car.ui

import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.SearchView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.utils.RouterPath
import com.example.module_car.R
import com.example.module_car.adapter.CarAdapter
import com.example.module_car.databinding.ActivityCarBinding
import com.example.module_car.model.CarEntityItem
import com.example.module_car.ui.addcar.AddCarActivity
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path= RouterPath.CAR_ACTIVITY_UI)
class CarActivity : BaseActivity<CarViewModel,ActivityCarBinding>() {
    private lateinit var mCarAdapter: CarAdapter
    private val mViewModel : CarViewModel by viewModels()
    override val mLayoutResId: Int get() = R.layout.activity_car
    private val mList = mutableListOf(
        CarEntityItem(1, "沪 A88888", "张先生 | 特斯拉 Model 3", "1", 2019),
        CarEntityItem(2,"沪 B66666","李女士 | 比亚迪 汉","1",2019),
        CarEntityItem(3,"沪 C99999","王先生 | 保时捷 911","1",2019)
    )

    override fun initView() {
        super.initView()
        mCarAdapter = CarAdapter()
        mCarAdapter.setList(mList)
        mBinding.carRecyclerView.adapter = mCarAdapter
        mBinding.carRecyclerView.layoutManager = LinearLayoutManager(this)
    }

    override fun initData() {
        super.initData()
        searchCar()
        //返回
        goBack()
        mBinding.main.setOnClickListener {
            //隐藏键盘
            hideKeyboard(mBinding.searchEditText)
        }
    }

    override fun initListener() {
        super.initListener()
        //添加车辆
        mBinding.addButton.setOnClickListener{
            startActivity(Intent(this,AddCarActivity::class.java))
        }
    }

    private fun searchCar() {
        //点击回车搜索
        mBinding.searchEditText.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query.isNullOrEmpty()){
                    mCarAdapter.setList(mList)
                }else {
                    mViewModel.getQueryCarList(query.toInt())
                    mViewModel.mCarState.observeWithLifecycle(this@CarActivity){
                        when (it) {
                            is Empty -> {}
                            is Failed -> {
                               visibleImg()
                            }
                            Loading -> {}
                            Success -> {
                                mViewModel.mCarList.observe(this@CarActivity){
                                    mCarAdapter.addListItem(it)
                                    visibleRv()
                                }
                            }
                        }
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()){
                    mCarAdapter.clearList()
                    mCarAdapter.setList(mList)
                    visibleRv()
                }
                return  true
            }

        })
        mBinding.searchEditText.setOnSearchClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                //唤起键盘
                hideSoftKeyBoard(mBinding.searchEditText)
            }
        })
    }
    private fun goBack(){
        mBinding.back.setOnClickListener {
            onBackPressed()
        }
    }

    private fun visibleImg(){
        mCarAdapter.clearList()
        mBinding.textId.visibility = View.VISIBLE
        mBinding.imageNone.visibility = View.VISIBLE
        mBinding.carRecyclerView.visibility = View.GONE
    }
    private fun visibleRv(){
        mBinding.textId.visibility = View.GONE
        mBinding.imageNone.visibility = View.GONE
        mBinding.carRecyclerView.visibility = View.VISIBLE
    }
}